package com.coc.serviceimpl.system;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coc.core.utils.StringRelaUtils;
import com.coc.dao.system.SysPermissionDao;
import com.coc.entity.core.JsonResult;
import com.coc.entity.system.SysPermission;
import com.coc.entity.system.SysRole;
import com.coc.entity.system.SysRolePermission;
import com.coc.entity.system.SysUser;
import com.coc.service.system.SysPermissionService;
import com.coc.service.system.SysRolePermissionService;
import com.coc.service.system.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 权限表(SysPermission)表服务实现类
 *
 * @author songju
 * @since 2020-02-10 19:12:18
 */
@Service("sysPermissionService")
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionDao, SysPermission> implements SysPermissionService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;
    @Override
    public List<SysPermission> selectListByUser(Integer id) {
        return this.baseMapper.selectListByUser(id);
    }

    @Override
    public List<SysPermission> selectListByPath(String requestUrl) {
        return this.baseMapper.selectListByPath(requestUrl);
    }

    @Override
    public List<SysPermission> getPermitById() {
        SysUser sysUser = sysUserService.GetCurrUser();
        if (sysUser==null){
            return null;
        }
        List<SysPermission> permitList = this.baseMapper.getPermitById(sysUser.getId());
        return permitList;
    }
@Transactional
    @Override
    public JsonResult addPermit(String json) {
        String roleIds = JSONObject.parseObject(json).getString("roleIds");
        SysPermission sysPermission = JSONObject.parseObject(json, SysPermission.class);
        sysPermission.setInsertTime(new Date());
        boolean save = save(sysPermission);
        if (save){
            SysRolePermission sp=null;
            QueryWrapper<SysPermission> sqw = new QueryWrapper<>();
            sqw.eq("code",sysPermission.getCode());
            SysPermission one = getOne(sqw);
            if (StrUtil.isNotBlank(roleIds)&&roleIds.length()==1){
                sp=new SysRolePermission();
                sp.setPermitId(one.getId());
                sp.setRoleId(Integer.parseInt(roleIds));
                sysRolePermissionService.saveOrUpdate(sp);
            }else if (StrUtil.isNotBlank(roleIds)&&roleIds.length()>1){
                ArrayList<SysRolePermission> sur = new ArrayList<>();
                List<String> rls = StringRelaUtils.strToList(roleIds);
                for (String rl:rls){
                    sp=new SysRolePermission();
                    sp.setRoleId(Integer.parseInt(rl));
                    sp.setPermitId(one.getId());
                    sur.add(sp);
                }
                sysRolePermissionService.saveBatch(sur);
            }
        }


        return null;
    }
}